package com.ruoyi.oos.duty.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.oos.duty.domain.Duty;
import com.ruoyi.oos.duty.domain.DutyClasses;
import com.ruoyi.oos.duty.domain.DutyTurnShowBean;
import com.ruoyi.oos.duty.domain.DutyTurns;
import com.ruoyi.oos.duty.mapper.DutyClassesMapper;
import com.ruoyi.oos.duty.mapper.DutyMapper;
import com.ruoyi.oos.duty.mapper.DutyTurnsMapper;
import com.ruoyi.oos.duty.service.IDutyClassesService;
import com.ruoyi.oos.duty.service.IDutyTurnsService;

/**
 * 轮值倒班信息Service业务层处理
 * 
 * @author bulang
 * @date 2020-04-29
 */
@Service
public class DutyTurnsServiceImpl implements IDutyTurnsService 
{
    @Autowired
    private DutyTurnsMapper dutyTurnsMapper;
    
    @Autowired
    private DutyClassesMapper dutyClassesMapper;
    
    
    @Autowired
    private DutyMapper dutyMapper;
    
    @Autowired
	IDutyClassesService classesService;
   

    /**
     * 查询轮值倒班信息
     * 
     * @param id 轮值倒班信息ID
     * @return 轮值倒班信息
     */
    @Override
    public DutyTurns selectDutyTurnsById(Long id)
    {
        return dutyTurnsMapper.selectDutyTurnsById(id);
    }

    /**
     * 查询轮值倒班信息列表
     * 
     * @param dutyTurns 轮值倒班信息
     * @return 轮值倒班信息
     */
    @Override
    public List<DutyTurns> selectDutyTurnsList(DutyTurns dutyTurns)
    {
        return dutyTurnsMapper.selectDutyTurnsList(dutyTurns);
    }
    
    /**
       *  轮值列表
     * @return
     */
    @Override
    public List<DutyTurnShowBean> getDutyTurnsByOrder(DutyTurns dutyTurns){
    	Long dutyid =dutyTurns.getDutyId();
        List<Long> orderList = dutyTurnsMapper.selectOrderno(dutyid);
        List<DutyTurnShowBean> returnList = new  ArrayList<DutyTurnShowBean>();
    	 for (Long orderid : orderList) {
    		 DutyTurnShowBean bean = new DutyTurnShowBean();
    		 bean.setOrderid(orderid);
    		 List<DutyTurns>  turnsList = dutyTurnsMapper.selectDutyTurnsListByorderId(orderid);
    		 Map<String,Object> map = new LinkedHashMap<String,Object>();
    		 for (DutyTurns turns : turnsList) {
    			 map.put("data"+turns.getClassesId(), turns.getTeamId());
			}
    		 bean.setMap(map);
			returnList.add(bean);
		}
    	return returnList;
    }
    
    
    /**
     *  轮值列表
   * @return
   */
  @Override
  public List<DutyTurnShowBean> getDutyTurnsByOrderId(Long orderNum){
      List<Long> orderList = new ArrayList<Long>();
      orderList.add(orderNum);
      List<DutyTurnShowBean> returnList = new  ArrayList<DutyTurnShowBean>();
  	 for (Long orderid : orderList) {
  		 DutyTurnShowBean bean = new DutyTurnShowBean();
  		 bean.setOrderid(orderid);
  		 List<DutyTurns>  turnsList = dutyTurnsMapper.selectDutyTurnsListByorderId(orderid);
  		 Map<String,Object> map = new LinkedHashMap<String,Object>();
  		 for (DutyTurns turns : turnsList) {
  			 map.put("data"+turns.getClassesId(), turns.getTeamId());
			}
  		 bean.setMap(map);
			returnList.add(bean);
		}
  	return returnList;
  }
    
    
    
    

    /**
     * 新增轮值倒班信息
     * 
     * @param dutyTurns 轮值倒班信息
     * @return 结果
     */
    @Override
    public int insertDutyTurns(DutyTurns dutyTurns)
    {
        return dutyTurnsMapper.insertDutyTurns(dutyTurns);
    }

    /**
     * 修改轮值倒班信息
     * 
     * @param dutyTurns 轮值倒班信息
     * @return 结果
     */
    @Override
    public int updateDutyTurns(DutyTurns dutyTurns)
    {
        return dutyTurnsMapper.updateDutyTurns(dutyTurns);
    }

    /**
     * 删除轮值倒班信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDutyTurnsByIds(String ids)
    {
        return dutyTurnsMapper.deleteDutyTurnsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除轮值倒班信息信息
     * 
     * @param id 轮值倒班信息ID
     * @return 结果
     */
    public int deleteDutyTurnsById(Long id)
    {
        return dutyTurnsMapper.deleteDutyTurnsById(id);
    }

	@Override
	public int insertDutyTurnsNew(Integer data1, Integer data2, Integer data3,Integer data4, Integer orderno) {
		DutyTurns turns = new DutyTurns();
		turns.setClassesId((long)1);
		turns.setDutyId((long)1);
		turns.setOrderno((long)orderno);
		turns.setTeamId((long)data1);
		dutyTurnsMapper.insertDutyTurns(turns);
		DutyTurns turns2 = new DutyTurns();
		turns2.setClassesId((long)2);
		turns2.setDutyId((long)1);
		turns2.setOrderno((long)orderno);
		turns2.setTeamId((long)data2);
		dutyTurnsMapper.insertDutyTurns(turns2);
		DutyTurns turns4 = new DutyTurns();
		turns4.setClassesId((long)4);
		turns4.setDutyId((long)1);
		turns4.setOrderno((long)orderno);
		turns4.setTeamId((long)data4);
		dutyTurnsMapper.insertDutyTurns(turns4);
		DutyTurns turns3 = new DutyTurns();
		turns3.setClassesId((long)3);
		turns3.setDutyId((long)1);
		turns3.setOrderno((long)orderno);
		turns3.setTeamId((long)data3);
		return dutyTurnsMapper.insertDutyTurns(turns3);
	}

	@Override
	public int deleteDutyTurnsByOrderNo(Integer orderno) {
		   return dutyTurnsMapper.deleteDutyTurnsByOrderNo(orderno);
		
	}
	
	
	public DutyTurns getCurDutyTurns(Date  date,Long dutyid) {
		DutyTurns turn = new DutyTurns();
		turn.setDutyId(dutyid);
	 List<DutyTurns> turns  = dutyTurnsMapper.selectDutyTurnsList(turn);
	 
	 DutyClasses c = new DutyClasses();
	 c.setDutyId(dutyid.intValue());
	 List<DutyClasses> classes = dutyClassesMapper.selectDutyClassesList(c);
	 Duty duty=  dutyMapper.selectDutyById(dutyid);
	 Date starttime = duty.getStarttime();
	 List<Date> datelist = new ArrayList<Date>();
		for (DutyClasses class1 : classes) {
			String time =class1.getTime();
			Date date1 =DateUtils.getDateHHMMSSStr(time);
			datelist.add(date1);
		}
	 return this.getCurData(starttime, turns, classes,datelist, date);
	}
	
	
	public DutyTurns getCurData(Date beginTime,List<DutyTurns> dutyTurns, List<DutyClasses> dutyClases , List<Date> datelist,Date now) {
		Long a = (long) dutyTurns.size();//一个周期长度 15
		Long b = (long) dutyClases.size();//一天的周期 24/3
		Long times =( now.getTime()- beginTime.getTime())/(24*60*60*1000);
		Long zhouqi = a/b;
		Long endtime = times%zhouqi;
		//int endturn = (int) ((15*endtime/zhouqi));
		int curClasses = this.getClasses(now, dutyClases,datelist);
		Long curorderno = endtime+1;
		for (DutyTurns turns : dutyTurns) {
			  Long orderno = turns.getOrderno();
			  Long classesid= turns.getClassesId();
			  if(orderno == curorderno && curClasses == classesid) {
				  return turns;
			  }
		}
		return null;
		
	}
	
	private  int getClasses(Date date,List<DutyClasses> classes,List<Date> list) {
		Date nowdate = DateUtils.getDateHHMMSS(date);
		for (int i = 0; i < list.size(); i++) {
			Date date1 = list.get(i);
			Date date2 = list.get(i+1);
			Date date3 = list.get(list.size()-1);
			if(nowdate.getTime()>= date3.getTime()) {
				return list.size();
			} else if(nowdate.getTime() >= date1.getTime() &&  nowdate.getTime()< date2.getTime()) {
				return i+1;
			}else if (nowdate.getTime() <= date1.getTime()) {
				return 1;
			}
			
		}
		return 0;
	}

	@Override
	public List<Map<String,Object>> getdutyDetail(Long dutyId, String startTime, String endTime) {
		List<String> list = DateUtils.getDays(startTime, endTime);
		DutyClasses classes = new DutyClasses();
		classes.setDutyId(dutyId.intValue());
		List<Map<String,Object>> returnList = new ArrayList<Map<String,Object>>();
		List<DutyClasses> classesList = classesService.selectDutyClassesList(classes);//所有班(三班)
		List<DutyTurns> turns  = dutyTurnsMapper.selectDutyTurnsList(null);
		List<Date> datelist = new ArrayList<Date>();
		for (DutyClasses class1 : classesList) {
			String time =class1.getTime();
			Date date1 =DateUtils.getDateHHMMSSStr(time);
			datelist.add(date1);
		}
		
		
		 Duty duty=  dutyMapper.selectDutyById(1L);
		 Date starttime = duty.getStarttime();
		for (String date : list) {
			for (DutyClasses dutyClasses : classesList) {
				String times = date +" " + dutyClasses.getTime();
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("times", times);
				map.put("class", dutyClasses.getName());
				DutyTurns turns1 =	this.getCurData(starttime,turns,classesList,datelist,DateUtils.parseDate(times));
				map.put("team", turns1.getTeamId());
				returnList.add(map);
			}
		}
		// TODO Auto-generated method stub
		return returnList;
	}

	
	
	
	
	
}
